package cn.exrick.xboot.modules.base.controller.common;

import cn.exrick.xboot.common.utils.Base64DecodeMultipartFile;
import cn.exrick.xboot.common.utils.QiniuUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.UploadAndDownloadUtils;
import cn.exrick.xboot.common.vo.Result;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Exrickx
 */
@Slf4j
@RestController
@Api(description = "文件上传接口")
@RequestMapping("/xboot/upload")
@Transactional

public class UploadController {
    // 项目根路径下的目录  -- SpringBoot static 目录相当于是根路径下（SpringBoot 默认）
    public final static String UPLOAD_PATH_PREFIX = "static/uploadFile/";

    @Autowired
    private QiniuUtil qiniuUtil;

    @RequestMapping(value = "/file",method = RequestMethod.POST)
    @ApiOperation(value = "文件上传")
    public Result<Object> upload(@RequestParam(required = false) MultipartFile file,
                                 @RequestParam(required = false) String base64,
                                 HttpServletRequest request) {

        if(StrUtil.isNotBlank(base64)){
            // base64上传
            file = Base64DecodeMultipartFile.base64Convert(base64);
        }
        String result = null;
        String fileName = qiniuUtil.renamePic(file.getOriginalFilename());
        try {
            InputStream inputStream = file.getInputStream();
            //上传七牛云服务器
            result = qiniuUtil.qiniuInputStreamUpload(inputStream,fileName);
        } catch (Exception e) {
            log.error(e.toString());
            return ResultUtil.error(e.toString());
        }

        return ResultUtil.data(result);
    }

    @PostMapping("/uploadFile")
    @ApiOperation(value = "文件上传到本地")
    public Result<Object> upload(MultipartFile uploadFile, HttpServletRequest request){
        return ResultUtil.data(UploadAndDownloadUtils.uploadUtils(uploadFile,request)) ;
    }

    @RequestMapping("/download")
    @ApiOperation(value = "文件读取 参数 文件路径")
    public String downLoad(String filePath ,HttpServletResponse response) throws UnsupportedEncodingException {
        UploadAndDownloadUtils.downLoad(response,filePath);
        return null;
    }
   @PostMapping("/handelFile")
   @ApiOperation(value = "字符串转文件 参数需要转换的字符串")
   public String handelFile(String str ,HttpServletResponse response) throws UnsupportedEncodingException {
       String s = UploadAndDownloadUtils.handleStrToFile(str);
       return s;
   }
   //文件转字符串
    @PostMapping("/fileToStr")
    @ApiOperation(value = "文件转字符串 参数需要转换的字符串")
    public String fileToStr(String filePath) throws  Exception{
        return UploadAndDownloadUtils.handleFileToStr(filePath);
    }

    //删除文件
    @PostMapping("/deleteFile")
    public Result<Object> deleteFile(String filePath) throws  Exception{
        UploadAndDownloadUtils.deleteFile(filePath);
        return ResultUtil.success("删除成功");
    }
    //获取视频缩略图
    @PostMapping("/exportVideoImage")
    public void exportVideoImage(String filePath,HttpServletResponse response) throws  Exception{
       /* UploadAndDownloadUtils.fetchFrame(filePath ,response);*/
    }
    @GetMapping("/test")
    public Map test(){
        Map map=new HashMap();
        map.put("msg","success");
        return  map;
    }

}
